package dao;

import beans.BoardFile;
import db.DBQuery;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

public class BoardFileDAO extends CommonDAO<BoardFile> {

    public final static BoardFileDAO ME = new BoardFileDAO();

    @Override
    public String tableName() {
        return "wb_board_file";
    }

    public void updateBoardContent(List<String> updateShapeList, long board_id) {
        String sql = "update " + tableName() + " set board_content = ? where board_id=" + board_id + " limit 1";
        DBQuery.updateBlob(sql, updateShapeList);
    }

    public void deleteBoardFile(long board_id) {
        String sql = "delete from " + tableName() + " where board_id=?";
        DBQuery.update(sql, board_id);
    }

    public List<String> getBoardShapeList(long board_id) {
        String sql = "select * from " + tableName() + " where board_id=? limit 1";
        List<String> shapeList = new ArrayList<>();
        try {
            BoardFile boardFile = DBQuery.read_bean(BoardFile.class, sql, board_id);
            byte[] boardContent = boardFile.getBoard_content();
            if (boardContent != null && boardContent.length > 0) {
                ByteArrayInputStream byteInt = new ByteArrayInputStream(boardContent);
                ObjectInputStream objInt = new ObjectInputStream(byteInt);
                shapeList = (List<String>) objInt.readObject();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return shapeList;
    }

}
